﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text.RegularExpressions;
using System.Data;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net.Mime;
using System.IO;

namespace Shared
{

    public static class Emailer
    {
        public static void SendMessage(string sendTo, string sendSubject, string sendMessage, bool useSSL)
        {
            SendMessage(sendTo, sendSubject, sendMessage, null, useSSL);
        }
        /// <summary>
        /// Transmit an email message to a recipient without
        /// any attachments
        /// </summary>
        /// <param name="sendTo">Recipient Email Address</param>
        /// <param name="sendFrom">Sender Email Address</param>
        /// <param name="sendSubject">Subject Line Describing Message</param>
        /// <param name="sendMessage">The Email Message Body</param>
        /// <returns>Status Message as String</returns>

        public static void SendMessage(string sendTo, string sendSubject, string sendMessage, Byte[] bytes, bool useSSL)
        {
            //try
            //{
                MailMessage mail = new MailMessage();
                //SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                //mail.From = new MailAddress("paubustillo@gmail.com");
                SmtpClient SmtpServer = new SmtpClient();
                mail.To.Add(sendTo);
                mail.Subject = sendSubject;
                mail.Body = sendMessage + "\r\n" + "\r\n" + "Ajuntament de Son Servera";
                

                //PAU: Code comented that sends an image as body in html
                //AlternateView textAlternateView = AlternateView.CreateAlternateViewFromString (sendMessage, null, MediaTypeNames.Text.Plain); 

                //string htmlBody = "<html><body><h1>Picture</h1><br><img src=\"cid:image\"></body></html>";
                //AlternateView avHtml = AlternateView.CreateAlternateViewFromString (htmlBody, null, MediaTypeNames.Text.Html);


                //MemoryStream stream = new MemoryStream();
                //stream.Write(bytes, 0, bytes.Length);
                //stream.Seek(0, SeekOrigin.Begin);

                //LinkedResource lr = new LinkedResource(stream);
                //lr.ContentId = "image";
                //avHtml.LinkedResources.Add(lr);

                //mail.AlternateViews.Add(textAlternateView);
                //mail.AlternateViews.Add(avHtml);

                //SmtpServer.Port = 587;
                //SmtpServer.Credentials = new System.Net.NetworkCredential("paubustillo", "martapau");
                SmtpServer.EnableSsl = useSSL;

                SmtpServer.Send(mail);
               // MessageBox.Show("mail Send");


            //}
            //catch (Exception ex)
            //{
            //    //throw;
            //}

        }
        /// <summary>

        /// Confirm that an email address is valid

        /// in format

        /// </summary>

        /// <param name="emailAddress">Full email address to validate</param>

        /// <returns>True if email address is valid</returns>

        public static bool ValidateEmailAddress(string emailAddress)
        {
            try
            {
                string TextToValidate = emailAddress;
                Regex expression = new Regex(@"\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}");

                // test email address with expression
                if (expression.IsMatch(TextToValidate))
                {
                    // is valid email address
                    return true;
                }
                else
                {
                    // is not valid email address
                    return false;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// Transmit an email message with
        /// attachments
        /// </summary>
        /// <param name="sendTo">Recipient Email Address</param>
        /// <param name="sendFrom">Sender Email Address</param>
        /// <param name="sendSubject">Subject Line Describing Message</param>
        /// <param name="sendMessage">The Email Message Body</param>
        /// <param name="attachments">A string array pointing to the location   of each attachment</param>
        /// <returns>Status Message as String</returns>

        public static void SendMessageWithAttachment(string sendTo, string sendSubject, string sendMessage, string fileName, bool useSSL)
        {
            //try
            //{
                // validate email address
                bool bTest = ValidateEmailAddress(sendTo);

                //if (bTest == false)
                  //  return "Invalid recipient email address: " + sendTo;

                // Create the basic message
                MailMessage message = new MailMessage();
                message.To.Add(sendTo);
                message.Subject = sendSubject;
                message.Body = "GPS Route" + "\r\n" + "\r\n" + "Ajuntament de Son Servera";

                // The attachments array should point to a file location    
                // where
                // the attachment resides - add the attachments to the 
                // message
                MemoryStream gpsFileStream = new MemoryStream();
                StreamWriter gpsFileWriter = new StreamWriter(gpsFileStream);
                gpsFileWriter.Write(sendMessage);
                gpsFileWriter.Flush();
                gpsFileStream.Seek(0, SeekOrigin.Begin);
                message.Attachments.Add(new Attachment(gpsFileStream, "test.gpx"));                
                

                // create smtp client at mail server location
                //SmtpClient client = new SmtpClient("smtp.gmail.com");
                SmtpClient client = new SmtpClient();
                //client.Port = 587;
                //client.Credentials = new System.Net.NetworkCredential("paubustillo", "martapau");
                client.EnableSsl = useSSL;

                // send message
                client.Send(message);
                message.Dispose();

              //  return "Message sent to " + sendTo + " at " + 
             //   DateTime.Now.ToString() + ".";

            //}
            //catch (Exception ex)
            //{
            ////    return ex.Message.ToString();
            //}
        }
    }    
}


